/**
 * 
 */
package eu.digitalida.android.tutorial;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



/**
 * @author Ahadu
 *
 */
public class FlowerView extends SurfaceView implements SurfaceHolder.Callback {


	private Bitmap pertalTop ;
    private TrackerThread thread;
    
    
	

	private int x=20,y=20;
    private int viewWidth,viewHeight;
    private float petalTopWidth, petalTopHeight;
    
	public FlowerView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public FlowerView(Context context, int w, int h) {
		super(context);
		
		viewWidth = w;
        viewHeight = h;
        thread = new TrackerThread(getHolder(),this);
        getHolder().addCallback(this);
        setFocusable(true);
        pertalTop = BitmapFactory.decodeResource(getResources(), R.drawable.petal_1);
        petalTopHeight = pertalTop.getHeight();
        petalTopWidth = pertalTop.getWidth();
	}

	

	/* (non-Javadoc)
	 * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
	 */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		 thread.startrun(true);
	     thread.start();

	}

	/* (non-Javadoc)
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		thread.startrun(false);
        thread.stop();

	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		
        canvas.drawColor(Color.GREEN);//To make background 
        canvas.drawBitmap(pertalTop,x-(pertalTop.getWidth()/2),y-(pertalTop.getHeight()/2),null);
		super.onDraw(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float tx = event.getX();
		float ty = event.getY();
		
				
		
		switch(event.getAction()) { 
		case MotionEvent.ACTION_MOVE: 
//			if(tx > x && tx < petalTopWidth + x && ty > y && ty < petalTopHeight+y){
				x = (int)tx;
				y = (int)ty;
				
//			}
			return true; 
		 
		case MotionEvent.ACTION_UP:  
//			x = (int)tx;
//			y = (int)ty;
			break; 
		
		case MotionEvent.ACTION_DOWN: 
			if(tx > x && tx < petalTopWidth + x && ty > y && ty < petalTopHeight+y){
				x = (int)tx;
				y = (int)ty;
				return true;
			}
	
		default:
			break;
		}
		
		
		return false;
	
	}
	
	
	


}
